package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/cart")
public class CartApiController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Long skuNum,
                            HttpServletRequest request){
        //获取userId
        String userId = AuthContextHolder.getUserId(request);
        //有可能用户未登录
        if(StringUtils.isEmpty(userId)){
            //获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(skuId,skuNum,userId);
        //默认返回
        return Result.ok();
    }

    /**
     * 查看购物车列表
     * @param userId
     * @param userTempId
     * @return
     */
    @GetMapping("cartList")
    public Result cartList(String userId,String userTempId){
        //获取购物车列表
        List<CartInfo> list = cartService.cartList(userId,userTempId);
        //默认返回
        return Result.ok(list);
    }

    /**
     * 选中状态
     * @param skuId
     * @param isChecked
     * @param request
     * @return
     */
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Long isChecked,
                            HttpServletRequest request){
        //选中状态在登陆和未登录状态下都可以选中
        String userId = AuthContextHolder.getUserId(request);
        //有可能用户未登录
        if(StringUtils.isEmpty(userId)){
            //获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //调用服务层方法
        cartService.checkCart(userId,skuId,isChecked);
        //默认返回
        return Result.ok();
    }

    /**
     * 删除购物车选项
     * @param skuId
     * @param request
     * @return
     */
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request){
        //选中状态在登陆和未登录状态下都可以选中
        String userId = AuthContextHolder.getUserId(request);
        //有可能用户未登录
        if(StringUtils.isEmpty(userId)){
            //获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //调用服务层方法
        cartService.deleteCart(skuId,userId);
        return Result.ok();
    }

    //根据用户Id 来获取到购物车列表
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){
        //返回数据
        return cartService.getCartCheckedList(userId);
    }

    //删除购物车数据
    @GetMapping("delCart/{userId}")
    public Result delCart(@PathVariable String userId){
        //删除购物车
        cartService.delcart(userId);
        return Result.ok();
    }
}
